package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//the reference to the ControlLayer
/**
 * 
 * User interface - main menu
 * The entry-point for the application:
 * Create an instance of class MainMenuUI and the constructor starts the application
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 *                    
 * (KBH): 2008.10.24: Change af manimenuStart to if
 */
public class MainMenuUI
{
  
   private AddressBookUI adrUI;//references to other UI objects
   private DvdUI dvdUI;
   private LendingsUI lendUI; 
   
   //Constructor
   public MainMenuUI()
   {
       
   }
   
   /**
    * Returns the menu, from where the user can make a choise.
    */
   public void mainMenuStart()
   {
       System.out.print("\f");
       boolean exit=false;
       int[] allowedAnswers = {1,2,3,4};
        while (!exit) //! means while exit not is true (that is: false)
        {
            
            int choise = writeMainMenu();
            if(choise == 1){
                startAddressBook();
            }
            else if(choise == 2){ 
                startDvd();
            }
            else if(choise == 3){
                startLendings();
            }//end if
            else if(choise ==4){
                exit = true;
                writeEnd();
            }
            else if(!allowedAnswers.equals(choise)){
                System.out.println("\fInvalid choise, please try again");
            }//end else
        }//end while
    }//end start
   
   /**
    * prints range of choices and takes an input from user.
    */ 
   private int writeMainMenu()
    {       
            int choise = 0;
            Scanner keyboard = new Scanner(System.in);

            System.out.println("\f*** MainMenu ***");

            System.out.println(" (1) AddressBook");
            System.out.println(" (2) DVDs");
            System.out.println(" (3) Loan");
            System.out.println(" (4) Close");
            System.out.print("\n Make your choise: ");
            

            try
            {
                choise = keyboard.nextInt();                    
            }
            
            
            catch(Exception e)
            {   
                writeMainMenu();
                System.out.println("\fIt has to be a number, please try again");
                    
            }
            return choise;
   }
    
   /**
    * starts the selected user interfaces
    */
    private void startAddressBook()
    {
        adrUI= new AddressBookUI();
        adrUI.start();
        //the adrUI needs to have a reference to the address controller
        //main controller can return references to the other controllers
     
    }

    private void startDvd()
    {
        System.out.print("\f");
        dvdUI= new DvdUI();
        dvdUI.start();
        
        
        //the dvdUI needs to have a reference to the DVD controller
        //main controller can return references to the other controllers
     
    }
    
     private void startLendings()
    {
        lendUI = new LendingsUI();
        lendUI.start();
        
        //the lendUI needs to have a reference to the lendings controller
        //main controller can return references to the other controllers
     
    }
    
    /**
     * prints an end message before closing
     */
    private void writeEnd()
    {
        System.out.println(" The System is shutting down ");
    }
 }
